package studio.raptor.extractor.segment.service;

import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

/**
 * Created by liuyong8 on 2017/8/4.
 */
@Service
public class RedisCacheService {
    private static final Logger log = LoggerFactory.getLogger(RedisCacheService.class);

    @Autowired
    private StringRedisTemplate template;

    /**
     * 持久化消息体
     * @param objectKey businessNo
     * @param kindId 队列主题
     * @param content 报文
     */
    public void saveEventBody(String objectKey, Integer kindId, String content) {
        Objects.requireNonNull(objectKey);
        Objects.requireNonNull(kindId);
        String key = kindId + ":" + objectKey;
        template.opsForValue().setIfAbsent(key, content);
    }

    /**
     * 根据事件ID获取事件的消息体
     *
     * @param objectKey 订单id
     * @param kindId 消息主题
     * @return 如果消息存在则返回字符串形式的消息体，如果不存在则返回null
     */
    public String fetchMessageBody(String objectKey, Integer kindId) {
        Objects.requireNonNull(kindId);
        Objects.requireNonNull(objectKey);
        String key = kindId + ":" + objectKey;
        return template.opsForValue().get(key);
    }

}
